package com.atguigu.lease.web.app.controller.room;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "房间信息")
@RestController
@RequestMapping("/app/room")
public class RoomController {
    @Autowired
    private RoomInfoService roomInfoService;

    @Operation(summary = "分页查询房间列表")
    @GetMapping("pageItem")
    public Result<IPage<RoomItemVo>> pageItem(@RequestParam long current, @RequestParam long size, RoomQueryVo queryVo) {
        Page<RoomItemVo> roomItemVoPage = new Page<>(current, size);
        //增加了id字段，可以与根据公寓id分页查询房间列表接口共用。分页查询房间列表时id固定为null，SQL不使用id作为查询条件；
        roomInfoService.queryPageRoom(roomItemVoPage,queryVo,null);
        return Result.ok(roomItemVoPage);
    }

    @Operation(summary = "根据id获取房间的详细信息")
    @GetMapping("getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id,@RequestHeader("access_token")String token) {
        Claims claims =  JwtUtil.parseToken(token);
        Long userId = ((Integer) claims.get("userId")).longValue();
        RoomDetailVo roomDetailVo = roomInfoService.queryRoomById(id,userId);
        return Result.ok(roomDetailVo);
    }

    @Operation(summary = "根据公寓id分页查询房间列表")
    @GetMapping("pageItemByApartmentId")
    public Result<IPage<RoomItemVo>> pageItemByApartmentId(@RequestParam long current, @RequestParam long size, @RequestParam Long id) {
        Page<RoomItemVo> roomItemVoPage = new Page<>(current, size);
        //增加了RoomQueryVo对象，可以与分页查询房间列表接口共用。根据公寓id分页查询房间列表时，传递空的RoomQueryVo对象，SQL不使用RoomQueryVo作为查询条件；
        roomInfoService.queryPageRoom(roomItemVoPage,new RoomQueryVo(),id);
//        roomInfoService.queryPageRoomById(roomItemVoPage,id);
        return Result.ok(roomItemVoPage);
    }
}
